package serialzable;

import java.io.*;

/**
 * created by flyketch
 * 19-9-10 下午2:02
 */
public class TestPerson {
    public static void main(String[] args) throws IOException {
        // 序列化
        FileOutputStream fileOutputStream = new FileOutputStream(new File("").getAbsolutePath() + "/object.txt");
        try {
            ObjectOutputStream outputStream = new ObjectOutputStream(fileOutputStream);
            Person person = new Person("小明", 21);
            outputStream.writeObject(person);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 反序列化
        try (ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(new File("").getAbsolutePath()+"/object.txt"))) {
            Person person = (Person) objectInputStream.readObject();
            System.out.println("name:" + person.getName() + ",age:" + person.getAge());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();

        }
    }
}
